#include <iostream>

#include "protocol.hpp"

enum
{
    DIVERR = 1,
    MODERR,
    OPERR
};

class Calculator
{
public:
    static Response Calculation(const Request& rq)
    {
        Response rp;
        switch (rq._op)
        {
        case '+':
            rp._result = rq._a + rq._b;
            break;
        case '-':
            rp._result = rq._a - rq._b;
            break;
        case '*':
            rp._result = rq._a * rq._b;
            break;
        case '/':
            if (rq._a != 0) rp._result = rq._a / rq._b;
            else rp._status_code = DIVERR;
            break;
        case '%':
            if (rq._a != 0) rp._result = rq._a % rq._b;
            else rp._status_code = MODERR;
            break;
        default:
            rp._status_code = OPERR;
        }
        return rp;
    }

    static std::string server_cal(std::string& message)
    {
        std::string content;
        bool back = decode(message, content);
        if(!back) return "";
        Request rq;
        back = rq.Deserialization(content);
        if(!back) return "";
        Response rp = Calculation(rq);
        std::string ret;
        back = rp.Serialization(ret);
        if(!back) return "";
        ret = encode(ret);
        return ret;
    }
};